package com.cqut.forum.aspect.imp;

import java.util.Calendar;

import javax.annotation.Resource;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.cqut.forum.aspect.UserScoreAspect;
import com.cqut.forum.dao.UserDao;
import com.cqut.forum.entity.User;

/**
 * 用户积分AOP接口实现类
 * @author 杨巡
 *
 */
@Aspect
@Component
public class UserScoreAspectImp implements UserScoreAspect {
	@Resource
	private UserDao userDao;
	
	@AfterReturning(returning="user",pointcut="execution(public * com.cqut.forum.dao.imp.UserDaoImp.getUser(..))")
	public void scoreHandle(User user) {
		if(user==null){
			return;
		}
		Long loginTime = user.getUserLastLoginTime().getTime();
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE,   -1);
		cal.add(Calendar.HOUR_OF_DAY, 0);
		cal.add(Calendar.MINUTE, 0);
		cal.add(Calendar.SECOND, 0);
		Long yesterday = cal.getTime().getTime();
		
		if(loginTime>yesterday){
			return;
		}
		user.setUserScore(user.getUserScore()+1);
		userDao.update(user);
		System.out.println("===="+user.getUserName()+"今天第一次登录，加1积分======");
	}
}
